package services

//
import (
	"ContentSystem/internal/api/operate"
	"ContentSystem/internal/dao"
	"ContentSystem/internal/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type ContentCreateReq struct {
	Title          string        `json:"title" binding:"required" `
	Description    string        `json:"description" binding:"required" `
	Author         string        `json:"author" binding:"required"`
	VideoURL       string        `json:"video_url"`
	Thumbnail      string        `json:"thumbnail"`
	Category       string        `json:"category"`
	Duration       time.Duration `json:"duration"`
	Resolution     string        `json:"resolution"`
	FileSize       string        `json:"file_size"`
	Format         string        `json:"format"`
	Quality        int           `json:"quality"`
	ApprovalStatus int           `json:"approval_status"`
	CreatedAt      time.Time     `json:"created_at"`
	UpdatedAt      time.Time     `json:"updated_at"`
}

type ContentCreatResp struct {
	Message string `json:"message" binding:"required"`
}

func (c *CmsApp) ContentCreate(ctx *gin.Context) {

	var req ContentCreateReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	fmt.Println(req.Title)

	resp, err := c.operateAppClient.CreateContent(ctx, &operate.CreateContentReq{
		Content: &operate.Content{
			Title:       req.Title,
			Description: req.Description,
			Author:      req.Author,
			VideoUrl:    req.VideoURL,
			Thumbnail:   req.Thumbnail,
			Category:    req.Category,
			Resolution:  req.Resolution,
			Format:      req.Format,
		},
	})

	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	//flowData := map[string]interface{}{
	//	"content_id": ID,
	//}

	//data, _ := json.Marshal(flowData)

	//if err := c.flowService.Execute("content-flow", &goflow.Request{
	//	Body: data,
	//}); err != nil {
	//	ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	//	return
	//}

	// 响应
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "ok",
		"data":    resp,
	})
}

func (c *CmsApp) LocalCreate(ctx *gin.Context, req ContentCreateReq) string {
	contentDao := dao.NewContentDao(c.db)
	ID, err := contentDao.Create(model.ContentDetail{
		Title:          req.Title,
		Description:    req.Description,
		Author:         req.Author,
		VideoURL:       req.VideoURL,
		Thumbnail:      req.Thumbnail,
		Category:       req.Category,
		Duration:       req.Duration,
		Resolution:     req.Resolution,
		FileSize:       req.FileSize,
		Format:         req.Format,
		Quality:        req.Quality,
		ApprovalStatus: req.ApprovalStatus,
	})
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return ""
	}

	fmt.Println(ID)

	return "成功"
}
